package com.atguigu.system.service.impl;
import com.atguigu.model.system.SysMenu;
import com.atguigu.model.system.SysRoleMenu;
import com.atguigu.model.vo.AssginMenuVo;
import com.atguigu.model.vo.RouterVo;
import com.atguigu.system.common.utils.MenuHelper;
import com.atguigu.system.common.utils.RouterHelper;
import com.atguigu.system.exception.CustomerException;
import com.atguigu.system.mapper.SysMenuMapper;
import com.atguigu.system.mapper.SysRoleMenuMapper;
import com.atguigu.system.service.SysMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author yqr
 * @since 2023-06-07
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        //获取菜单列表
        List<SysMenu> sysMenuList = this.list();

        //判断菜单是否为空
        if(CollectionUtils.isEmpty(sysMenuList)){
            return null;
        }

        //利用递归，重新构建菜单结构---树型结构
        return MenuHelper.buildTree(sysMenuList);
    }

    @Override
    public void removeMenuById(String id) {
        LambdaQueryWrapper<SysMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysMenu::getParentId,id);
        int count = this.count(lqw);
        if (count > 0){
            throw new CustomerException(201,"所选项包含子菜单,不能直接删除!");
        }
        this.removeById(id);
    }

    @Override
    public List<SysMenu> distributionById(String id) {
        //获取所有状态为1菜单----menu表
        LambdaQueryWrapper<SysMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysMenu::getStatus,1);
        List<SysMenu> menuList = this.list(lqw);

        //根据传入的角色id 获取 角色拥有的菜单数据----role_menu表
        LambdaQueryWrapper<SysRoleMenu> roleMenuLqw = new LambdaQueryWrapper<>();
        roleMenuLqw.eq(SysRoleMenu::getRoleId,id);
        List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(roleMenuLqw);

        //从角色菜单数据集合中,获取角色拥有的菜单id集合
        List<String> menuIdList = new ArrayList<>();
        for (SysRoleMenu sysRoleMenu:sysRoleMenus) {
            String menuId = sysRoleMenu.getMenuId();
            menuIdList.add(menuId);
        }

        //在所有菜单中筛选出，用户已经勾选的菜单,设置其setSelect为true
        for (SysMenu sysMenu:menuList) {
            String id1 = sysMenu.getId();
            //contains 包含，包含返回true，未包含返回false
            sysMenu.setSelect(menuIdList.contains(id1));
        }

        //将数据转为树型结构并返回
        return MenuHelper.buildTree(menuList);
    }

    @Override
    public void updatePermissions(AssginMenuVo assginMenuVo) {
        //删除该户用，之前所有的权限
        String roleId = assginMenuVo.getRoleId();
        LambdaQueryWrapper<SysRoleMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysRoleMenu::getRoleId,roleId);
        sysRoleMenuMapper.delete(lqw);

        //根据用户勾选的菜单权限id集合,重新添加权限
        List<String> menuIdList = assginMenuVo.getMenuIdList();
        for (String menuId : menuIdList) {
            if (menuId !=null) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenu.setMenuId(menuId);
                //添加新权限
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        }
    }

    @Override
    public List<RouterVo> getUserMenuList(String userId) {
        //声明集合
        List<SysMenu> sysMenuList = null;

        //判断是否是超级管理员,是则查询全部，不是则三表查询
        if ("1".equals(userId)){
            LambdaQueryWrapper<SysMenu> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SysMenu::getStatus,1);
            lqw.orderByAsc(SysMenu::getSortValue);
            sysMenuList = this.list(lqw);
        }else {
            sysMenuList = sysMenuMapper.selectMenuList(userId);
        }

        //构建树型结构数据
        List<SysMenu> treeMenu = MenuHelper.buildTree(sysMenuList);

        //转换为前端路由格式的数据
        return RouterHelper.buildRouters(treeMenu);
    }


    @Override
    public List<String> getUserButtonList(String userId) {
        //声明集合
        List<SysMenu> sysMenuList = null;

        //判断是否为超级管理员
        if("1".equals(userId)){
            LambdaQueryWrapper<SysMenu> lqw = new LambdaQueryWrapper<>();
            lqw.eq(SysMenu::getStatus,1);
            lqw.orderByAsc(SysMenu::getSortValue);
            sysMenuList = sysMenuMapper.selectList(lqw);
        }else {
            sysMenuList = sysMenuMapper.selectMenuList(userId);
        }

        //遍历集合,获取按钮权限信息,放入一个新集合中
        List<String> userButtonList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            //判断是否为按钮
            if (sysMenu.getType() == 2){
                //获取按钮权限信息
                String perms = sysMenu.getPerms();
                //加入到集合中
                userButtonList.add(perms);
            }
        }
        return userButtonList;
    }
}
